package
{
	import MathText.*;
	
	import TaskStructure.IdScreen;
	import TaskStructure.Task;
	import TaskStructure.XMLLoader;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.sampler.NewObjectSample;
	
	[SWF(backgroundColor="#000000", width="1244", height="700", frameRate="50")]
	//[SWF(backgroundColor="#000000", widthPercent="100%", heightPercent="90%", frameRate="50")]
	public class Main extends Sprite
	{
		public var userID:String, projectID:String, startingProblemNumber:int;
		// public objects
		public var taskFlowXML:XML;
		public var contextXML:XML;
		public var problemXML:XML;
		public var imagesXML:XML;
		public var errorsXML:XML;
		//private objects
		private var xmlLoader:XMLLoader;
		private var task:Task;
		private var idScreen:IdScreen;
		
		public function Main()
		{
			this.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
			idScreen = new IdScreen();
			idScreen.addEventListener(Event.COMPLETE, handleIdScreenComplete);
			this.addChild(idScreen);
		}
		private function handleIdScreenComplete (event:Event):void
		{
			idScreen.removeEventListener(Event.COMPLETE, handleIdScreenComplete);
			userID = idScreen.userID;
			projectID = idScreen.projectID;
			startingProblemNumber = idScreen.problemNumber;
			this.removeChild(idScreen);
			xmlLoader = new XMLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, handleFlowLoaderComplete);
			if (projectID.length > 0)
			{
				xmlLoader.init(String("ProjectFiles/"+projectID));
			} else 
			{
				xmlLoader.init("ProjectFiles/default.xml");
			}
		}
		private function handleFlowLoaderComplete (event:Event):void
		{
			xmlLoader.removeEventListener(Event.COMPLETE, handleFlowLoaderComplete);
			taskFlowXML = xmlLoader.xml;
			xmlLoader = new XMLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, handleProblemLoaderComplete);
			xmlLoader.init(String("xmlDocs/"+taskFlowXML.@problemTemplatesXML));
		}
		private function handleProblemLoaderComplete (event:Event):void
		{	
			xmlLoader.removeEventListener(Event.COMPLETE, handleProblemLoaderComplete);
			problemXML = xmlLoader.xml;
			xmlLoader = new XMLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, handleContextLoaderComplete);
			xmlLoader.init(String("xmlDocs/"+taskFlowXML.@contextTextXML));
		}
		private function handleContextLoaderComplete (event:Event):void
		{	
			xmlLoader.removeEventListener(Event.COMPLETE, handleContextLoaderComplete);	
			contextXML = xmlLoader.xml;
			// get image names xml
			xmlLoader = new XMLLoader(); 
			xmlLoader.addEventListener(Event.COMPLETE, handleImagesLoaderComplete);
			xmlLoader.init(String("xmlDocs/"+taskFlowXML.@contextImagesXML));
		}
		private function handleImagesLoaderComplete (event:Event):void
		{
			xmlLoader.removeEventListener(Event.COMPLETE, handleImagesLoaderComplete);
			imagesXML = xmlLoader.xml;
			// get image names xml
			xmlLoader = new XMLLoader(); 
			xmlLoader.addEventListener(Event.COMPLETE, handleErrorLoaderComplete);
			xmlLoader.init(String("xmlDocs/"+taskFlowXML.@errorFeedbackXML));
		}
		private function handleErrorLoaderComplete (event:Event):void
		{
			xmlLoader.removeEventListener(Event.COMPLETE, handleErrorLoaderComplete);
			errorsXML = xmlLoader.xml;
			complete();
		}
		
		private function complete ():void
		{
			task = new Task(userID, startingProblemNumber, taskFlowXML, problemXML, contextXML, imagesXML, errorsXML);
			task.addEventListener(Event.COMPLETE, handleTaskComplete);
			this.addChild(task);
		}
		private function handleTaskComplete (event:Event):void
		{
			task.removeEventListener(Event.COMPLETE, handleTaskComplete);
		}
	}
}